/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Form1.java
 *
 * Created on Aug 20, 2011, 1:56:11 PM
 */
package cw.ui;

import cw.entity.CrossFields;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;

/**
 *
 * @author VinhTC
 */
public class GeneratePuzzleInterface extends javax.swing.JFrame {

    /** Creates new form Form1 */
    public GeneratePuzzleInterface() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jtxtName = new javax.swing.JTextField();
        jbtnSend = new javax.swing.JButton();
        jcmbSubject = new javax.swing.JComboBox();
        jtxtSubject = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jpnPuzzle = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jbtnSend.setText("Send");
        jbtnSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnSendActionPerformed(evt);
            }
        });

        jcmbSubject.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jcmbSubject.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jcmbSubjectItemStateChanged(evt);
            }
        });
        jcmbSubject.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcmbSubjectActionPerformed(evt);
            }
        });

        jpnPuzzle.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder("Puzzle")));
        jpnPuzzle.setToolTipText("Setting Puzzle");
        jpnPuzzle.setName("Puzzle"); // NOI18N

        javax.swing.GroupLayout jpnPuzzleLayout = new javax.swing.GroupLayout(jpnPuzzle);
        jpnPuzzle.setLayout(jpnPuzzleLayout);
        jpnPuzzleLayout.setHorizontalGroup(
            jpnPuzzleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 424, Short.MAX_VALUE)
        );
        jpnPuzzleLayout.setVerticalGroup(
            jpnPuzzleLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 203, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(jpnPuzzle);
        jpnPuzzle.getAccessibleContext().setAccessibleName("Puzzle");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 446, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jtxtSubject, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
                            .addComponent(jcmbSubject, javax.swing.GroupLayout.Alignment.LEADING, 0, 137, Short.MAX_VALUE)
                            .addComponent(jtxtName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnSend)
                        .addGap(367, 367, 367))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(63, 63, 63)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtxtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jbtnSend))
                .addGap(18, 18, 18)
                .addComponent(jcmbSubject, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jtxtSubject, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
                .addGap(13, 13, 13))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnSendActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jbtnSendActionPerformed

    private void jcmbSubjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcmbSubjectActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jcmbSubjectActionPerformed

    private void jcmbSubjectItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jcmbSubjectItemStateChanged
        jtxtSubject.setText(jcmbSubject.getSelectedItem().toString());
//        Form1 f= new Form1();
        List<Integer> row = new ArrayList<Integer>();
        List<Integer> col = new ArrayList<Integer>();

        if (0 == 1) {
            //Size of list is Leng of answer
            for (int i = 0; i < 3; i++) {
                row.add(1);//value of PositonX from database
                col.add(i);
            }
        } else {
            //Size of list is Leng of answer
            for (int i = 0; i < 3; i++) {
                row.add(i);
                col.add(1);//value of PositonY from database
            }
        }
        setTextpanel(jcmbSubject.getSelectedIndex() + 2, jcmbSubject.getSelectedIndex() + 2, row, col);
    }//GEN-LAST:event_jcmbSubjectItemStateChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                new GeneratePuzzleInterface().setVisible(true);

            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnSend;
    private javax.swing.JComboBox jcmbSubject;
    private javax.swing.JPanel jpnPuzzle;
    private javax.swing.JTextField jtxtName;
    private javax.swing.JTextField jtxtSubject;
    // End of variables declaration//GEN-END:variables

    JTextField[][] wo;
    int x = -1, y = -1;


    private void setTextpanel(final int size1, int size2, List<Integer> row, List<Integer> col) {

        jpnPuzzle = new JPanel(new GridLayout(size1, size2, 5, 5));
        wo = new JTextField[size1][size2];
        System.out.print(size1);
        
        for (int i = 0; i < size1; i++) {
            for (int j = 0; j < size2; j++) {
                wo[i][j] = new CrossFields(j, i);
                wo[i][j].setFont(new java.awt.Font("Arial", 1, 20));

                for (int k = 0; k < row.size(); k++) {
                    for (int l = 0; l < col.size(); l++) {
                        if (i == row.get(k) && j == col.get(l)) {
                            wo[i][j].setBackground(new Color(10, 12, 15));
                        }
                    }
                }

                wo[i][j].setForeground(new Color(102, 0, 0));
                wo[i][j].setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
                wo[i][j].setHorizontalAlignment(SwingConstants.CENTER);
                wo[i][j].addMouseListener(new MouseAdapter() {
                    @Override
                    public void mousePressed(MouseEvent e) {
                        CrossFields a = (CrossFields) e.getSource();
                        for (int k = 0; k < size1; k++) {
                            for (int l = 0; l < size1; l++) {
                                wo[k][l].setText("");
                            }
                        }
                        a.setText("X");
                        x = a.getPoX();
                        y = a.getPoY();
                    }

                });
                wo[i][j].addKeyListener(new KeyAdapter() {

                    @Override
                    public void keyTyped(KeyEvent e) {

                        System.out.println("Key pressed: "+ e.getKeyChar());
                    }
                    
                });
                
                jpnPuzzle.add(wo[i][j]);
            }
        }
        jScrollPane1.setViewportView(jpnPuzzle);
    }
}
